/**
 * 
 */
package fr.upvm.lem3.vm3d.pdfPrinter;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.swing.GroupLayout.Alignment;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import fr.upvm.lem3.vm3d.domain.Channel;
import fr.upvm.lem3.vm3d.domain.Header;

/**
 *******************************************************************
 *SimplePDFPrinter.java
 *26.03.2012
 *@author Guy Nougouapi
 *******************************************************************
 *print the rendered 3d volume with some related information in PDF format.
 *One can customize this class on its needs
 *******************************************************************
 */
public class SimplePDFPrinter {

	/**
	 * the header of the current loaded 3d file
	 */
	Header file3dHeader;
	/**
	 * the current visualized channel
	 */
	Channel<?> channel;
	/**
	 * image to draw
	 */
	BufferedImage bImage;
	/**
	 * 
	 */
	int[] planeValues;
	
	public SimplePDFPrinter(Header file3dheader, Channel<?> channel, BufferedImage bImage, 
																int[] planeVal, File file){
		this.file3dHeader = file3dheader;
		this.channel = channel;
		this.bImage = bImage;
		this.planeValues = new int[3];
		this.planeValues = planeVal;

		Document document = new Document(PageSize.A4);     // create a A4 size document
		try {
			PdfWriter.getInstance(document, new FileOutputStream(file));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			System.out.println(e.getCause());
		} catch (DocumentException e) {
			e.printStackTrace();
			System.out.println(e.getCause());
		}
		
		document.open();
		// TODO: Add code to create the content of the document here
		try {
			//Add the buffered image
			Paragraph paraName = new Paragraph(file.getName().substring(0, file.getName().length()-4)+"\n");
			paraName.setAlignment(Element.ALIGN_CENTER);
			document.add(paraName);
			document.add(new Paragraph("\n\n"));
			
			//Add the Image
			com.itextpdf.text.Image image = com.itextpdf.text.Image.getInstance(bImage, null);
			PdfPTable tableImage = new PdfPTable(1);
			tableImage.addCell(image);
			document.add(tableImage);
			document.add(new Paragraph("\n"));
			
			//Add the channel
			PdfPTable table1 = new PdfPTable(2);
			String channelValue = "Channel: "+channel.getName().toUpperCase()+
										" ["+ channel.getLowerLimit()+" , "+channel.getUpperLimit()+ "]";
			PdfPCell cell1 = new PdfPCell(new Paragraph(channelValue));	
			cell1.setColspan(2);
			table1.addCell(cell1);
			document.add(table1);
			document.add(new Paragraph("\n"));
			
			//Add the Value of the planes
			PdfPTable table2 = new PdfPTable(3);
			PdfPCell cell2 = new PdfPCell(new Paragraph((new String("Plane Values")).toUpperCase()));	
			cell2.setColspan(3);
			table2.addCell(cell2);
			table2.addCell("XY");
			table2.addCell("XZ");
			table2.addCell("YZ");
			table2.addCell(String.valueOf(planeVal[0]));
			table2.addCell(String.valueOf(planeVal[1]));
			table2.addCell(String.valueOf(planeVal[2]));
			document.add(table2);
			document.add(new Paragraph("\n"));
			
			//Add the values of the dimensions
			PdfPTable table3 = new PdfPTable(3);
			PdfPCell cell3 = new PdfPCell(new Paragraph((new String("Dimensions")).toUpperCase()));	
			cell3.setColspan(3);
			table3.addCell(cell3);
			table3.addCell("Z");
			table3.addCell("Y");
			table3.addCell("X");
			table3.addCell(String.valueOf(file3dheader.getzCells()));
			table3.addCell(String.valueOf(file3dheader.getyCells()));
			table3.addCell(String.valueOf(file3dheader.getxCells()));
			document.add(table3);
			document.add(new Paragraph("\n"));
			//Add the values of the steps
			
			PdfPTable table4 = new PdfPTable(3);
			PdfPCell cell4 = new PdfPCell(new Paragraph((new String("Steps")).toUpperCase()));	
			cell4.setColspan(3);
			table4.addCell(cell4);
			table4.addCell("Z");
			table4.addCell("Y");
			table4.addCell("X");
			table4.addCell(String.valueOf(file3dheader.getzStep()));
			table4.addCell(String.valueOf(file3dheader.getyStep()));
			table4.addCell(String.valueOf(file3dheader.getxStep()));
			document.add(table4);
			
		} catch (DocumentException | IOException e) {
			e.printStackTrace();
		}
		// Close the document
		document.close();
	}
	
}
